package com.wuad.platformwuad.module.study.classDemo;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 类及类字段操作方法
 *
 * 获取对象中的属性及属性值
 *
 * @author wuad
 * @date 2023年08月21日 9:37
 */
@Slf4j
public class ClassProcessingController {

    PersonEntity personEntity = new PersonEntity("张三", 18, "等级1");

    /**
     * 遍历实体类属性及属性值
     */
    @Test
    public void reflect() throws IllegalAccessException {
        Class cls = personEntity.getClass();
        System.out.println("类名：" + cls.getName()); // com.platform.platformwad.platform.studyModule.classProcessingDemo.PersonEntity
        System.out.println("SimpleName:" + cls.getSimpleName()); // PersonEntity
        System.out.println("CanonicalName:" + cls.getCanonicalName());
        Field[] fields = cls.getDeclaredFields();
        for(int i=0; i<fields.length; i++){
            Field f = fields[i];
            f.setAccessible(true);
            System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(personEntity));
        }
    }

    /**
     * 动态的修改字段值
     */
    @Test
    public void setValue() {
        try {
            Field field = PersonEntity.class.getDeclaredField("name");
            Field field1 = PersonEntity.class.getDeclaredField("age");

            //setAccessible这行代码把对象上的name字段设置为可访问
            field.setAccessible(true);
            field1.setAccessible(true);
            System.out.println("返回字段名称：" + field.getName());
            System.out.println("返回字段属性值：" + field.get(field.getName()));
            System.out.println("Modifier after modify:"+field + " " +field.getModifiers());

            // 设置该user1的name属性
            field.set(personEntity, "zhangsan");
            field1.set(personEntity, 11);
            log.info("修改后的实体：{}", personEntity);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 通过实体类的属性名获取对应的属性值
     *
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Test
    public void findValueByProperty() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        String fieldName = "name";
        Method method2 = personEntity.getClass().getMethod(getGetMethod(personEntity.getClass(), fieldName));
        Object value2 = null;
        value2 = method2.invoke(personEntity, new Class[]{});
        log.info("这是通过字段名称获取字段值：{}", value2);
    }

    /**
     * 动态获取实体类的get，set方法
     *
     * @param objectClass
     * @param fieldName
     * @return
     *
     */
    public static String getGetMethod(Class objectClass, String fieldName) {
        StringBuffer sb = new StringBuffer();
        sb.append("get");
        sb.append(fieldName.substring(0, 1).toUpperCase());
        sb.append(fieldName.substring(1));
        try {
            log.info("xxx: {}", objectClass.getMethod(sb.toString()).getName());
            return sb.toString();
        } catch (Exception e) {
            log.info("出错啦：{}", e.getMessage());
        }
        return null;
    }


    @Test
    public void getValue() throws NoSuchFieldException, IllegalAccessException {
        PersonEntity obj = new PersonEntity("张三", 18, "等级1");
        Field field = obj.getClass().getDeclaredField("name");
        field.setAccessible(true);  // 设置可访问私有字段
        Object fieldValue = field.get(obj);
        System.out.println(fieldValue);  // 输出字段值
    }


}
